package acm.蓝桥17;

import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
public class Main {
    static int[][] arr1 = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
    static int[][] arr2 = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 }, { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } };
    static int N;
    static int M;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (sc.hasNext()) {
            M = sc.nextInt();
            N = sc.nextInt();
            int[][] arr = new int[M+2][N+2];
            for (int i = 1; i < M+1; i++) {
                String str = sc.next();
                for (int j = 1; j < N+1; j++) {
                    arr[i][j] = Integer.parseInt(str.charAt(j-1) + "");
                }
            }
            System.out.println(sol(arr, M, N));
        }
    }
    public static int sol(int[][] arr, int M, int N) {
        int count = 0;
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[] { 0, 0 });
        arr[0][0]=2;
        while (!q.isEmpty()) {
            int[] temp = q.poll();
            int X = temp[0];
            int Y = temp[1];
            for (int i = 0; i < 8; i++) {
                int Xi = arr2[i][0]+X;
                int Yi = arr2[i][1]+Y;
                if (Xi >= 0 && Xi < M+2 && Yi >= 0 && Yi < N+2 && arr[Xi][Yi] != 2) {
                    if (arr[Xi][Yi] == 1) {
                        count++;
                        di(Xi, Yi, arr);
                    }else{
                        arr[Xi][Yi] =2;
                        q.add(new int[] { Xi, Yi });
                    }
                }
            }
        }
        return count;
    }
    public static void di(int i, int j, int[][] arr) {
        arr[i][j] = 2;
        for (int i1 = 0; i1 < 4; i1++) {
            int Xi = arr1[i1][0]+i;
            int Yi = arr1[i1][1]+j;
            if (Xi >= 0 && Xi < M+2 && Yi >= 0 && Yi < N+2 && arr[Xi][Yi] == 1) {
                di(Xi,Yi,arr);
            }
        }
    }
}
